package templatejbdc;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.List;
import java.util.Properties;

public class DruidDemo {
    public static void main(String[] args) throws Exception {
        //使用数据连接池获取连接对象
        Properties properties = new Properties();
        InputStream in = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        properties.load(in);
        //生成数据源
        DataSource dataSouce = DruidDataSourceFactory.createDataSource(properties);
        //使用jdbctemplate注入数据源
        JdbcTemplate template = new JdbcTemplate(dataSouce);
        //连接
        //Connection connection = dataSouce.getConnection();
        // System.out.println(connection);
        String sql = "SELECT *\n" +
                "FROM `student`";
        List<Student> list = template.query(sql, new BeanPropertyRowMapper(Student.class));
        for (Student s : list) {
            System.out.println(s);
        }
    }
}
